/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sarco.bwi.lib;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;

/**
 * @since v0.0.5a
 * @author Andre
 */
public class FileManagement {

    public static Config loadConfig(String config) {
        Config conf = new Config();
        conf.loadConfig(config);
        return conf;
    }

    public static Mysql loadMysql(String host,
            String username, String password, String database) {
        Mysql mysql = new Mysql();
        mysql.connect(host, username, password, database);
        return mysql;
    }
    
    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }
    
    public static void downloadFile(String url, String output) throws IOException {
        // Http download:
        if (url.startsWith(FileManagement.HTTP)) {
            Socket client = new Socket(url, 80);
            FileOutputStream fout = new FileOutputStream(new File(output));
            int b = -1;
            while ((b = client.getInputStream().read()) != -1) {
                fout.write(b);
            }
            fout.close();
        }
    }
    
    private static final String HTTP    = "http://";
    private static final String HTTPS   = "https://";
}
